package com.lening.sellergoods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lening.mapper.TbItemCatMapper;
import com.lening.pojo.TbItemCat;
import com.lening.pojo.TbItemCatExample;
import com.lening.sellergoods.service.ItemCatService;
import entity.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 创作时间：2019/11/25 9:14
 * 作者：李增强
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Resource
    private TbItemCatMapper tbItemCatMapper;


    @Override
    public List<TbItemCat> findAll() {
        return tbItemCatMapper.selectByExample(null);
    }

    @Override
    public List<TbItemCat> findAll(long pid) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        return tbItemCatMapper.selectByExample(example);
    }

    @Override
    public TbItemCat findOne(long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(TbItemCat entity, Long pid) {
        if(entity!=null){
            String ids = entity.getIds();
            String typeId = ids.substring(ids.indexOf(":")+1, ids.indexOf(","));
            entity.setTypeId(Long.valueOf(typeId));

            if(entity.getId()!=null){
                tbItemCatMapper.updateByPrimaryKey(entity);
            }else{
                entity.setParentId(pid);
                tbItemCatMapper.insert(entity);
            }
        }
    }

    List<Long> ids=new ArrayList<Long>();
    @Override
    public void deleteByIds(Long[] idss) {
        if(idss!=null&&idss.length>=1){
            for (Long id : idss) {
                hh(id);     //通过递归拿到无限循环字节下的所有字节的id保存到List集合中
            }
            System.out.println(ids);
            for (Long id1 : ids) {
                tbItemCatMapper.deleteByPrimaryKey(id1);
            }
        }
    }

    @Override
    public PageResult findPage(Integer pageNum, Integer pageSize, Long pid) {
        PageHelper.startPage(pageNum,pageSize);
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        Page<TbItemCat> page = (Page<TbItemCat>) tbItemCatMapper.selectByExample(example);
        return new PageResult(page.getTotal(),page.getResult());
    }



    /**
     * 我们删除无限极列表，删除父亲，必须把他的子节点全删除
     * 我们拿到id，先拿这个id去数据库查询他有没有子节点
     */

    public void hh(long id){
        ids.add(id);
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        if(list.size()==0){
        }else{
            for (TbItemCat cat : list) {
                hh(cat.getId());
            }
        }
    }

}
